﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utility;

namespace Utility
{
    public static class GHStd
    {
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="no_Type">号码类别:1—用户号，2—速记号</param>
        /// <param name="iccard_No">IC卡号</param>
        /// <param name="error">ref 返回错误信息</param>
        /// <returns>返回ICCardCustomerModel对象类型</returns>
        public static object GetCustomerInfo(string no_Type, string iccard_No, ref string error)
        {
            object obj = null;
            string bao = GHInterface.CombineInputHead(eGHTradeType.ICCardCustomerSearch, GHInterface.CombineInputBody(eGHTradeType.ICCardCustomerSearch, "", no_Type, iccard_No, "", "", ePayMode.No), "", GLOBAL.BankOperator, "", "", "", GLOBAL.BankNo);
            if (string.IsNullOrWhiteSpace(bao))
            {
                return obj;
            }
            obj = GetSocketResponse(eGHTradeType.ICCardCustomerSearch, bao, ref error);
            return obj;
        }

        /// <summary>
        /// IC 卡预充值，获取预充值金额
        /// </summary>
        /// <param name="user_No">户号</param>
        /// <param name="iccard_No">IC卡号</param>
        /// <param name="payType">付款类型</param>
        /// <param name="payAmount">购买气量数</param>
        /// <param name="error">ref 返回错误信息</param>
        /// <returns>返回ICCardPrePayModel对象类型</returns>
        public static object GetPrePayMoney(string iccard_No, ePayMode payType, string payAmount, ref string error)
        {
            object obj = null;
            string bao = GHInterface.CombineInputHead(eGHTradeType.ICCardPrePay, GHInterface.CombineInputBody(eGHTradeType.ICCardPrePay, "", "", iccard_No, payAmount, "", payType), "", GLOBAL.BankOperator, "", "", "", GLOBAL.BankNo);
            if (string.IsNullOrWhiteSpace(bao))
            {
                return obj;
            }
            obj = GetSocketResponse(eGHTradeType.ICCardPrePay, bao, ref error);
            return obj;
        }

        /// <summary>
        /// IC卡确认充值
        /// </summary>
        /// <param name="user_No">户号</param>
        /// <param name="iccard_No">IC 卡号</param>
        /// <param name="payType">付款类型</param>
        /// <param name="payFee">缴费金额</param>
        /// <param name="payAmount">购买气量数</param>
        /// <param name="error">ref 返回错误信息</param>
        /// <returns>返回ICCardConfirmModel对象类型</returns>
        public static object ConfirmPay(string iccard_No, ePayMode payType, string payFee, string payAmount, string flowNo, ref string error)
        {
            object obj = null;
            string bao = GHInterface.CombineInputHead(eGHTradeType.ICCardConfimPay, GHInterface.CombineInputBody(eGHTradeType.ICCardConfimPay, "", "", iccard_No, payAmount, payFee, payType, flowNo), "", GLOBAL.BankOperator, "", "", "", GLOBAL.BankNo);
            if (string.IsNullOrWhiteSpace(bao))
            {
                return obj;
            }
            obj = GetSocketResponse(eGHTradeType.ICCardConfimPay, bao, ref error);
            return obj;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="no_Type"></param>
        /// <param name="iccard_No"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public static object GetHisPayRecord(string no_Type, string iccard_No, ref string error)
        {
            object obj = null;
            string bao = GHInterface.CombineInputHead(eGHTradeType.ICCardHisPayRecord, GHInterface.CombineInputBody(eGHTradeType.ICCardHisPayRecord, "", no_Type, iccard_No, "", "", ePayMode.No), "", GLOBAL.BankOperator, "", "", "", GLOBAL.BankNo);
            if (string.IsNullOrWhiteSpace(bao))
            {
                return obj;
            }
            obj = GetSocketResponse(eGHTradeType.ICCardHisPayRecord, bao, ref error);
            return obj;
        }

        public static object SendAccountRequest(string fileName, int fileCount, ref string error)
        {
            object obj = null;
            string bao = GHInterface.CombineInputHead(eGHTradeType.ICCardBalanceAccount, GHInterface.CombineInputBody(eGHTradeType.ICCardBalanceAccount, fileName, fileCount.ToString(), "", "", "", ePayMode.No), "",GLOBAL.BankOperator, "", "", "",GLOBAL.BankNo);
            if (string.IsNullOrWhiteSpace(bao))
            {
                return obj;
            }
            obj = GetSocketResponse(eGHTradeType.ICCardBalanceAccount, bao, ref error);
            return obj;
        }

        private static object GetSocketResponse(eGHTradeType type, string bao, ref string error)
        {
            object obj = null;
            string responseBao = SocketClient.SendRequest(bao, ref error);
            if (!string.IsNullOrWhiteSpace(responseBao) && string.IsNullOrWhiteSpace(error))
            {
                if (GHInterface.AnalysisOutputHead(type, responseBao, ref error))
                {
                    obj = GHInterface.AnalysisiOutputBody(type, responseBao);
                }
            }
            if (!string.IsNullOrWhiteSpace(error))
            {
                WriteLog.WriteLogFile(string.Format("get socket response error ，bao：{0}，error：{1}", bao, error));
            }
            return obj;
        }
    }

    /// <summary>
    /// 港华接口-济华项目
    /// </summary>
    internal static class GHInterface
    {
        /// <summary>
        /// 组合输入报文头
        /// </summary>
        /// <param name="tradeType">交易类型</param>
        /// <param name="inputBody">组合好的输入包体</param>
        /// <param name="area_code">镇区代码</param>
        /// <param name="bank_oper_id">银行操作员号</param>
        /// <param name="spot_no">网点代码</param>
        /// <param name="re_code">结果代码</param>
        /// <param name="re_msg">结果描述</param>
        /// <param name="bank_no">银行代码</param>
        /// <returns>包头</returns>
        internal static string CombineInputHead(eGHTradeType tradeType, string inputBody, string area_code, string bank_oper_id, string spot_no, string re_code, string re_msg, string bank_no)
        {
            string head = null;
            try
            {
                int body_Length = inputBody.Length;
                head = (78 + body_Length).ToString().PadRight(5, ' ') + (int)tradeType + bank_no + area_code.PadRight(2, ' ') + bank_oper_id.PadRight(10, ' ') + spot_no.PadRight(10, ' ') + re_code.PadRight(4, ' ') + re_msg.PadRight(40, ' ') + inputBody;
            }
            catch (Exception ex)
            {
                head = null;
                WriteLog.WriteLogFile("combine input head exception：" + ex.Message);
            }
            return head;
        }

        /// <summary>
        /// 组合输入包体内容
        /// </summary>
        /// <param name="tradeType">交易类型</param>
        /// <param name="user_No">户号或者速记号</param>
        /// <param name="no_Type">号码类别，1用户号，2速记号</param>
        /// <param name="iccard_No">IC卡号</param>
        /// <param name="pay_Amount">购买气量</param>
        /// <param name="payMoney">购买金额</param>
        /// <param name="pm">付款方式</param>
        /// <returns></returns>
        internal static string CombineInputBody(eGHTradeType tradeType, string user_No, string no_Type, string iccard_No, string pay_Amount, string payMoney, ePayMode pm, string flow_No = "")
        {
            string result = null;
            try
            {
                switch (tradeType)//除金额右对齐外，其他均左对齐；
                {
                    case eGHTradeType.ICCardCustomerSearch://IC卡客户信息查询
                        {
                            result = user_No.PadRight(30, ' ') + no_Type + iccard_No.PadRight(30, ' ');
                        }
                        break;
                    case eGHTradeType.ICCardPrePay://IC卡预充值
                        {
                            result = user_No.PadRight(30, ' ') + iccard_No.PadRight(30, ' ') + "0".PadLeft(13, ' ') + (int)pm + "".PadRight(20, ' ') + pay_Amount.PadRight(13, ' ') + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ("L" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + user_No).PadRight(20, ' ').Substring(0, 20) + "".PadRight(13, ' ');
                        }
                        break;
                    case eGHTradeType.ICCardConfimPay://IC卡确认充值
                        {
                            result = flow_No.PadRight(20, ' ') + user_No.PadRight(30, ' ') + iccard_No.PadRight(30, ' ') + (int)pm + "".PadRight(20, ' ') + 0 + payMoney.PadLeft(13, ' ') + pay_Amount.PadRight(13, ' ') + System.DateTime.Now.ToString("yyyyMMddHHmmss");
                        }
                        break;
                    case eGHTradeType.ICCardPayment://IC卡缴费冲正
                        break;
                    case eGHTradeType.ICCardHisPayRecord://IC卡历史缴费记录查询
                        {
                            result = user_No.PadRight(30, ' ') + no_Type + iccard_No.PadRight(30, ' ');
                        }
                        break;
                    case eGHTradeType.ICCardBalanceAccount://IC卡对账请求
                        {
                            result = user_No.PadRight(30, ' ') + "Y" + no_Type.PadRight(8, ' ');
                        }
                        break;
                    case eGHTradeType.ICCardBackBalanceAccount://银行对账
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                result = null;
                WriteLog.WriteLogFile("combine input body exception：" + ex.Message);
            }
            return result;
        }

        /// <summary>
        /// 分析返回报文结果头，如果错误，直接不分析包体部分
        /// </summary>
        /// <param name="tradeType">交易类型</param>
        /// <param name="body">返回的包</param>
        /// <param name="errMsg">返回错误信息</param>
        /// <returns>true为解析成功，false为解析失败或者包内容就是失败的</returns>
        internal static bool AnalysisOutputHead(eGHTradeType tradeType, string body, ref string errMsg)
        {
            bool result = false;
            errMsg = null;
            if (string.IsNullOrWhiteSpace(body))
            {
                return result;
            }
            int tran_code = Convert.ToInt32(body.Substring(5, 5));
            string re_code = body.Substring(34, 4);
            if (tran_code != (int)tradeType || re_code != "0000")
            {
                errMsg = body.Substring(38, 40);
                return result;
            }
            else
            {
                result = true;
            }
            return result;
        }

        /// <summary>
        /// 分析返回来的包体内容
        /// </summary>
        /// <param name="tradeType">交易类型</param>
        /// <param name="body">返回的整体包</param>
        /// <returns>返回一个Object</returns>
        internal static object AnalysisiOutputBody(eGHTradeType tradeType, string body)
        {
            object result = null;
            try
            {
                int startIndex = 78;
                switch (tradeType)//除金额右对齐外，其他均左对齐；
                {
                    case eGHTradeType.ICCardCustomerSearch://IC卡客户信息查询
                        {

                        }
                        break;
                    case eGHTradeType.ICCardPrePay://IC卡预充值
                        {

                        }
                        break;
                    case eGHTradeType.ICCardConfimPay://IC卡确认充值
                        {

                        }
                        break;
                    case eGHTradeType.ICCardPayment://IC卡缴费冲正
                        break;
                    case eGHTradeType.ICCardHisPayRecord://IC卡历史缴费记录查询
                        {

                        }
                        break;
                    case eGHTradeType.ICCardBalanceAccount://IC卡对账请求
                        {
                            result = body.SubString(38, 40);
                        }
                        break;
                    case eGHTradeType.ICCardBackBalanceAccount://银行对账
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                result = null;
                WriteLog.WriteLogFile("analysisi output body exception：" + ex.Message);
            }
            return result;
        }
    }
}
